﻿using App.Dal;
using App.Model;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.Role
{
    public partial class FrmList : UIPage
    {
        int isFirstLoad = 0;
        int TotalCount = 0;
        int CurrentPageSize = 20;
        int CurrentPageIndex = 0;
        int CurrentRowIndex = -1;
        private QueryPageModel queryModel;
        public QueryPageModel QueryModel
        {
            get
            {
                if (queryModel == null)
                {
                    queryModel = new QueryPageModel();
                }
                queryModel.KeyWord = txtKeyWord.Text.Trim();
                return queryModel;
            }

            set
            {
                queryModel = value;
            }
        }

        public FrmList()
        {
            InitializeComponent();
            InitGridViewColumns();
            InitQuery();
        }

        /// <summary>
        /// 初始化GridView
        /// </summary>
        public void InitGridViewColumns()
        {
            dataGridView.AutoGenerateColumns = false;
            dataGridView.ReadOnly = true;
            dataGridView.MultiSelect = false;
            dataGridView.RowHeadersVisible = false;
            dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            var columns = Config.ModelToDataGridColumns<SysRole>(new SysRole());
            dataGridView.Columns.Clear();
            dataGridView.Columns.AddRange(columns.ToArray());
        }

        public void InitQuery()
        {
            txtKeyWord.Text = "";
            GetList(1, CurrentPageSize, true);
        }

        private void FrmSamplingData_Resize(object sender, EventArgs e)
        {
            if (isFirstLoad > 0)
            {
                var panls = this.GetAllControls().Where(m => m is TabPage).ToList();
                panls.ForEach(item =>
                {
                    var frm = item.GetAllControls().Where(m => m is Form).FirstOrDefault();
                    if (frm != null)
                    {
                        var tempForm = (UIPage)frm;
                        tempForm.FormBorderStyle = FormBorderStyle.None;
                        tempForm.TopLevel = false;
                        tempForm.Dock = DockStyle.Fill;
                        tempForm.WindowState = FormWindowState.Maximized; //如果windowState设置为最大化，添加到tabPage中时，winform不会显示出来
                        tempForm.Dock = DockStyle.Fill;
                        //pane.Controls.Add(frm);
                        tempForm.Refresh();
                    }
                });
            }
            isFirstLoad++;
        }

        public void GetList(int pageIndex, int pageSize, bool setTotalCount = false)
        {
            pageData.PageSize = CurrentPageSize;
            QueryModel.PageIndex = pageIndex;
            QueryModel.PageSize = pageSize;

            var data = SysRoleServices.GetPageList(QueryModel, ref TotalCount);
            if (setTotalCount)
                pageData.TotalCount = TotalCount;
            dataGridView.DataSource = data;
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int pageSize)
        {
            CurrentPageIndex = pageIndex;
            GetList(CurrentPageIndex, pageSize);
        }

        private void dataGridView_SelectIndexChange(object sender, int index)
        {
            CurrentRowIndex = index;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            GetList(1, CurrentPageSize, true);
        }

        private void FrmSamplingData_Load(object sender, EventArgs e)
        {
            queryModel = new QueryPageModel();
            GetList(1, CurrentPageSize, true);
            BindCombox();
        }
        public void BindCombox()
        {

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmEdit frmEdit = new FrmEdit();
            frmEdit.ShowDialog();
            GetList(1, CurrentPageSize, true);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex < 0)
            {
                UIMessageBox.ShowWarning("请选择一行");
                return;
            }
            var id = dataGridView.Rows[CurrentRowIndex].Cells["Id"].Value.ToIntEx();
            FrmEdit edit = new FrmEdit(id);
            edit.ShowDialog();
            GetList(CurrentPageIndex, CurrentPageSize, false);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex < 0)
            {
                UIMessageBox.ShowWarning("请选择一行");
                return;
            }
            var id = dataGridView.Rows[CurrentRowIndex].Cells["Id"].Value.ToIntEx();
            var name = dataGridView.Rows[CurrentRowIndex].Cells["DepartName"].Value.ToStringEx();
            if (UIMessageBox.ShowAsk($"确认删除 [ {name} ]？"))
            {
                if (SysRoleServices.Delete(id))
                {
                    GetList(CurrentPageIndex, CurrentPageSize, true);
                    UIMessageBox.Show("删除成功");
                }
                else
                    UIMessageBox.ShowError("删除失败");
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            InitQuery();
        }

    }
}
